package club.beimeng.eduservice.controller;


import club.beimeng.commonutils.R;
import club.beimeng.eduservice.entity.EduTeacher;
import club.beimeng.eduservice.entity.vo.TeacherQuery;
import club.beimeng.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-16
 */
@Api(tags = "讲师管理模块")
@RestController /*含义：只返回数据*/
@CrossOrigin    //解决跨域问题
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    /*注入service*/
    @Autowired
    private EduTeacherService eduTeacherService;

    /*1 查询讲师表所有数据*/
    //rest风格
    @ApiOperation("所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        /*调用service的方式实现查询所有的操作,因为是继承的所有已经有封装好的代码了*/
        List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("items",list);
    }

    /*2 逻辑删除讲师的方法*/
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        return eduTeacherService.removeById(id) ? R.ok() : R.error();
//        if (eduTeacherService.removeById(id)) {
//            return R.ok();
//        }else{
//            return R.error();
//        }
    }

    /*3 分页查询讲师*/
    @ApiOperation("实现讲师查询分页")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
        //创建page对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current,limit);
        //调用方法实现分页
        eduTeacherService.page(eduTeacherPage);

        long total = eduTeacherPage.getTotal(); /*总记录数*/
        List<EduTeacher> records = eduTeacherPage.getRecords(); /*数据list集合*/
        return R.ok().data("total",total).data("rows",records);
    }

    /*4 条件查询带分页功能*/
    @ApiOperation("多条件组合查询带分页功能")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable Long limit,@RequestBody(required = false) TeacherQuery teacherQuery){
        //创建配置对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        //构建条件wrapper
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
            //多条件组合查询，可能都有也可能没有,动态sql
        String name = teacherQuery.getName();
        String level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        //添加默认排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal(); /*总记录数*/
        List<EduTeacher> records = pageTeacher.getRecords(); /*数据list集合*/
        return R.ok().data("total",total).data("rows",records);
    }

    /*5 添加讲师界接口*/
    @ApiOperation("添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? R.ok() :R.error();
    }

    // 根据讲师id进行查询
    @ApiOperation("通过id进行查询")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    /*6 讲师修改功能,先查询讲师id*/
    // RequestBody 需要和PostMapping绑定使用
    @ApiOperation("讲师修改功能")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@ApiParam("need id") @RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        return flag ? R.ok() : R.error();
    }
}

